/*
 * @(#)DataBaseChangeListener.java   1.0  2021年11月17日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dao.db.datasource;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.swift.core.env.ConfigInterface.ChangeConfigKV;
import com.swift.core.env.ConfigInterface.ConfigChangeListener;
import com.swift.util.type.TypeUtil;

/**
 * 添加说明
 * 
 * @author zhengjiajin
 * @version 1.0 2021年11月17日
 */
@Component
public class DataBaseChangeListener implements ConfigChangeListener<DataBase> {

    @Autowired
    private DataSourceFactory dataSourceFactory;
    /**
     * @see com.swift.core.env.ConfigInterface.ConfigChangeListener#listener(java.util.List)
     */
    @Override
    public void listener(List<ChangeConfigKV<DataBase>> list) {
        if (TypeUtil.isNull(list)) return;
        for (ChangeConfigKV<DataBase> db : list) {
            if (TypeUtil.isNull(db.getNewConfig())) {
                dataSourceFactory.delDataSource(db.getOldConfig().getKey());
            } else if (TypeUtil.isNull(db.getOldConfig())) {
                DataBase newDb = db.getNewConfig();
                dataSourceFactory.addDataSource(newDb.getKey(), newDb.getUrl(), newDb.getUserName(),
                    newDb.getPassword(), newDb.getDriverClassName(), newDb.getMaxPoolSize(), newDb.getMinPoolSize());
            } else {
                dataSourceFactory.delDataSource(db.getOldConfig().getKey());
                DataBase newDb = db.getNewConfig();
                dataSourceFactory.addDataSource(newDb.getKey(), newDb.getUrl(), newDb.getUserName(),
                    newDb.getPassword(), newDb.getDriverClassName(), newDb.getMaxPoolSize(), newDb.getMinPoolSize());
            }
        }

    }

}
